import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Users from '../components/Users.vue'
import Rights from '../components/Rights.vue'

const Roles = () => import('../components/Roles')


Vue.use(VueRouter)

const routes = [{
	path: '/login',
	component: Login
},
{
	path: '/',
	redirect: '/login'
},
{
	path: '/home',
	component: Home,
	children: [{
		path: '/users',
		component: Users
	},
	{
		path: '/roles',
		component: Roles
	},
	{
		path: '/rights',
		component: Rights
	}]


}
]


const router = new VueRouter({
	mode: 'history',
	routes
})

router.beforeEach((to, from, next) => {
	if (to.path === '/login') return next();

	const tokenStr = window.sessionStorage.getItem('token')

	//获取token
	if (!tokenStr) {
		return next('/login');
	} else {
		next()
	}

})


export default router
